﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

/// <summary>
/// 效果对象
/// </summary>
public class EffectObj
{
	public GameObject obj;	//对象
	public TSprite sprite;	//sprite
	public string name;		//保存的名字类别
	public bool free;		//是否是空闲状态
	public float time;		//开始时间
}


/// <summary>
/// 子弹对象管理类
/// </summary>
public class BulletManager : MonoBehaviour {
	
	static Dictionary<string, GameObject> effectObjList = new Dictionary<string, GameObject>();	//已经加载资源完成之后的 对象 （id, effectObj）
	
	
	
	static List<EffectObj> list = new List<EffectObj>();		//保存的对象列表
	
	static BulletManager instance;
	public static BulletManager GetInstance()
	{
		return instance;
	}
	
	void Awake()
	{
		instance = this;	
	}
	
	public void OnUpdate()
	{
		for(int i = 0; i < list.Count; i++)
		{
			if(!list[i].free)
			{
				list[i].sprite.OnUpdate();
			}
		}
	}
	
	/// <summary>
	/// 如果当前子弹是空闲的状态， 并且对象是显示的，则 改为空闲的状态
	/// </summary>
	void CheckShowObj(EffectObj obj)
	{
		if(obj.free && obj.obj.activeInHierarchy)
		{
			SetFreeEffectObj(obj.name, obj);
		}
	}
	
	/// <summary>
	/// 根据名字获得空闲的EffectObj 对象
	/// </summary>
	public static EffectObj GetFreeEffectObj(string name)
	{
		foreach(var o in list)
		{
			if(o.name.IndexOf(name) >= 0 && o.free)
			{
				o.free = false;			//设置为使用状态
				o.obj.collider.enabled = true;
				o.obj.SetActive(true);
				return o;
			}
		}
		
		var obj = GetEffectObjModle(name);	//如果没有的话，添加一条新的数据
		
		if(obj != null)
		{
			var t = Instantiate(obj) as GameObject;
			t.transform.parent = obj.transform.parent;
			t.transform.localPosition = Vector3.zero;
			t.transform.localRotation = Quaternion.Euler(Vector3.zero);
			UISprite sprite = t.GetComponent<UISprite>();
			sprite.MakePixelPerfect();
			var e = CreateEffectObj(name, t, t.GetComponent<TSprite>());
			
			e.free = false;
			e.obj.collider.enabled = true;
			e.obj.SetActive(true);
			return e;
		}
		
		return null;
	}
	
	/// <summary>
	/// 设置 一个对象为空闲状态
	/// </summary>
	public static void SetFreeEffectObj(string name, EffectObj obj)
	{
		obj.free = true;
		obj.name = name;
		obj.obj.transform.localPosition = Vector3.zero;
		obj.obj.collider.enabled = false;
		obj.obj.SetActive(false);
	}
	
	/// <summary>
	/// 创建 EffectObj 对象
	/// </summary>
	public static EffectObj CreateEffectObj(string name, GameObject obj = null, TSprite sprite = null)
	{
		EffectObj e = new EffectObj();
		if(obj == null)
			obj = new GameObject();
		e.obj = obj;
		e.name = name;
		e.free = true;
		e.time = Time.realtimeSinceStartup;
		e.sprite = sprite;
		if(obj.collider != null)
			obj.collider.enabled = false;
		list.Add(e);
		
		return e;
	}
	
	/// <summary>
	/// 添加效果模板数据
	/// </summary>
	public static void AddEffectObjModel(string name, GameObject obj)
	{
		if(effectObjList.ContainsKey(name))
			effectObjList.Remove(name);
		effectObjList.Add(name, obj);
	}
	
	/// <summary>
	/// 获得效果模板数据
	/// </summary>
	public static GameObject GetEffectObjModle(string name)
	{
		if(effectObjList.ContainsKey(name))
			return effectObjList[name];
		else
			return null;
	}
	
	/// <summary>
	/// 清除缓存对象数据
	/// </summary>
	public static void Clear()
	{
		list.Clear();
		effectObjList.Clear();
	}
}
